{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Temporal water level changes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook will draw graphs for temporal water level changes.\n",
    "My idea is drawing separate graph for reference ground track (RFT). There are 7th cycles so far.\n",
    "I will plot the water level of each cycle.\n",
    "\n",
    "Steps are:\n",
    "1. sepeate files by RFT\n",
    "2. draw all cycless for each RFT"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Codes for starting this notebook from the previous notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import glob\n",
    "import os\n",
    "import pandas as pd\n",
    "from pathlib import Path\n",
    "import h5py\n",
    "import re\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib widget\n",
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "##### load files\n",
    "## set the directory\n",
    "data_home = Path('/home/jovyan/ICESat_water_level/extraction/download/')\n",
    "## list them up and check them\n",
    "files= list(data_home.glob('*.h5'))\n",
    "## choose a file as an example\n",
    "\n",
    "##### function of reading alt13\n",
    "def alt13_to_df(filename, beam):    \n",
    "    f = h5py.File(filename, 'r')\n",
    "    f_beam = f[beam]\n",
    "    lat = f_beam['segment_lat'][:]\n",
    "    long = f_beam['segment_lon'][:]\n",
    "    ws = f_beam['ht_water_surf'][:]\n",
    "    ws_sd = f_beam['stdev_water_surf'][:]\n",
    "    ws_err = f_beam['err_ht_water_surf'][:]\n",
    "    ortho = f_beam['ht_ortho'][:]\n",
    "    wd = f_beam['water_depth'][:]\n",
    "    alt13_df = pd.DataFrame({'Latitude':lat,'Longitude':long,'SurfaceH':ws,\n",
    "                            'SH_SD':ws_sd, 'SH_error':ws_err,'OrthoH':ortho,\n",
    "                            'WaterD':wd})\n",
    "    return alt13_df\n",
    "\n",
    "### We set 'gt2l' as a beam of example for tracking\n",
    "D_dict={}\n",
    "error_count=0\n",
    "for ff in files:\n",
    "    try:\n",
    "        D_dict[ff]=alt13_to_df(ff, 'gt2l')\n",
    "    except KeyError as e:\n",
    "        ##print(f'file {ff} encountered error {e}')\n",
    "        error_count += 1\n",
    "print(f\"read {len(D_dict)} data files of which {error_count} gave errors\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### bounds of Tonle Sap Lake\n",
    "sp_ex = [103.643, 104.667, 12.375, 13.287]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The file structure is:\n",
    "- Examle: ATL13_2019031211225_09690201_003_01.h5\n",
    "- `ATL13` = product\n",
    "- `2019` = year\n",
    "- `0312` = data\n",
    "- `11225` = time\n",
    "- `0969` = RGT\n",
    "- `02` = cycle\n",
    "- `01` = region\n",
    "- `003` = data release\n",
    "- `01` = version"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get the unique RGT: {'0529', '0025', '0901', '0017', '0085', '0969', '0961', '0461', '0521'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rgt_l = []\n",
    "for dd in range(0,len(list(D_dict))):\n",
    "    list_dd = str(list(D_dict)[dd])\n",
    "    rgt_inf = list_dd.split(\"_\")[5][0:4]\n",
    "    rgt_l.append(rgt_inf)\n",
    "print(rgt_l)    \n",
    "uniq_l = set(rgt_l)\n",
    "print(uniq_l)\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Separating groups"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### new dictioray\n",
    "new_dict = {}\n",
    "for dd in range(0,len(list(D_dict))):\n",
    "    list_dd = str(list(D_dict)[dd])\n",
    "    rgt_inf = list_dd.split(\"_\")[5]\n",
    "    new_dict.update({rgt_inf: list(D_dict.values())[dd]})\n",
    "print(new_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "select RGT \"0961\" for mapping"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "##print(new_dict.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 0961\n",
    "list_0969 = [4,8,12,23,33,36]\n",
    "list_0961 = [2,7,18,25,28,35]\n",
    "list_0461 = [0,3,22,24,29]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Function for plotting photons"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import h5py\n",
    "import numpy as np\n",
    "import cartopy.crs as ccrs\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib as mpl        \n",
    "import cartopy.io.img_tiles as cimgt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_photons (se_list, in_dic_key, in_dic_cal, waterL, title, bound):\n",
    "    test_dict = {}\n",
    "    for i in se_list:\n",
    "        listed = str(list(in_dic_key.keys())[i])[4:6]\n",
    "        test_dict.update({listed:list(in_dic_cal.values())[i]})\n",
    "        \n",
    "    fig=plt.figure(figsize=(6,4))\n",
    "    ax = fig.add_subplot(111)\n",
    "    for ii in list(test_dict):\n",
    "        ax.plot(test_dict[ii]['Longitude'],test_dict[ii][waterL],markersize=0.25, label=ii)\n",
    "\n",
    "    h_leg=ax.legend()\n",
    "    plt.title(title)\n",
    "    ax.set_xlabel('Longitude')\n",
    "    ax.set_ylabel(waterL)\n",
    "    plt.show()\n",
    "    ## function for mapping the water level \n",
    "    def ic2_map_wl2(dicts, bound, water_levels):\n",
    "        fig = plt.figure(figsize=(6,4))\n",
    "        ax = plt.axes(projection=ccrs.PlateCarree())\n",
    "        ax.set_extent(bound, crs=ccrs.PlateCarree())\n",
    "        for i in dicts:\n",
    "            plt.scatter(dicts[i]['Longitude'], dicts[i]['Latitude'], s=10, c=dicts[i][water_levels], alpha=.7, transform=ccrs.PlateCarree(), cmap='terrain')\n",
    "        plt.colorbar(fraction=0.0320, pad=0.02, label='Elevation (m)')\n",
    "        ##load Google Sat.Map\n",
    "        #request = cimgt.GoogleTiles(style='satellite')\n",
    "        request = cimgt.Stamen('terrain-background')\n",
    "        ax.add_image(request, 7)\n",
    "        plt.title(title)\n",
    "    ic2_map_wl2(test_dict,bound,waterL)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_photons(list_0969,new_dict,D_dict,'OrthoH','OrthoH_0969',sp_ex)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_photons(list_0969,new_dict,D_dict,'SurfaceH','0969',sp_ex)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_photons(list_0961,new_dict,D_dict,'SurfaceH','0961',sp_ex)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Need to do\n",
    "1. Which water level among surface water height and orthometric height is chose\n",
    "2. There are so many gaps. This is because of different locations despite of same rgt, according to the map. What can we do?\n",
    "    - Need to do additional literature reviews for tracking temporal water levels using ICESat1/2\n",
    "    - Need to find futher official documents."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
